import axios from "axios"
const debug = process.env.NODE_ENV !== 'production'
const instance = axios.create({
    baseURL: debug ? 'api' : "https://www.freepandanovel.com/v1/",
    timeout: 10000,
})

// 请求拦截器   
instance.interceptors.request.use(config => {
    let token = localStorage.getItem("token");
    if (token) {
        config.headers = config.headers || {};
        config.headers.Authorization = `JWT ${token}`;
    }
    // config.headers.Authorization = `JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxMDEwNDE5LCJ1c2VybmFtZSI6IjAwMDAwMDAwLTRjYTctMDM4ZS1mZmZmLWZmZmZjYTAxZmRmNCIsImV4cCI6MTY4OTA2ODI5NiwiZW1haWwiOm51bGx9.mhZ7tW08uDn2gPgk73Euv4mijijwbMUavvZY32DbxEQ`;
    return config
}, err => {
    return Promise.reject(err)
})

// // 响应拦截器
instance.interceptors.response.use(res => {
    if(res.data.code == 401){
        localStorage.removeItem('token')
    }
    return res.data
}, err => {
    return Promise.reject(err)
})
export default instance